package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.builder.web.PageViewBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.DateUtils;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateLogRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateQueryRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateCancelRequest;
import com.fulihui.wgj.admin.api.wxtemplate.vo.SendTemplateDetailVO;
import com.fulihui.wgj.admin.api.wxtemplate.vo.SendTemplateLogVO;
import com.fulihui.wgj.admin.integration.SendTemplateLogServiceClient;
import com.fulihui.wgj.facade.dto.WxSendTemplateLogDTO;
import com.fulihui.wgj.facade.request.PrimaryKeyRequest;
import com.fulihui.wgj.facade.request.template.SendTemplateLogCancelRequest;
import com.fulihui.wgj.facade.request.template.SendTemplateLogPageQueryRequest;
import com.fulihui.wgj.facade.result.template.SendTemplateLogPageResult;
import com.fulihui.wgj.facade.service.template.SendTemplateLogService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: Levon
 * @date: 2018-08-07 15:33
 */
@Service
public class SendTemplateLogServiceClientImpl implements SendTemplateLogServiceClient {

    @Reference(version = "1.0.0")
    private SendTemplateLogService sendTemplateLogService;

    @Override
    public PageView<SendTemplateLogVO> sendTemplateLog(SendTemplateLogRequest request) {

        List<SendTemplateLogVO> templateLogVOS = Lists.newArrayList();

        SendTemplateLogPageQueryRequest queryRequest = BeanConv.copy(request, SendTemplateLogPageQueryRequest.class);
        queryRequest.setAppId(request.getAppId());
        queryRequest.setRows(request.getRows());
        queryRequest.setPage(request.getPage());
        SendTemplateLogPageResult pageResult = sendTemplateLogService.pageQuerySendLogs(queryRequest);
        if (RpcResultCheckUtil.isSuccess(pageResult)) {
            List<WxSendTemplateLogDTO> values = pageResult.getValue();
            if (!CollectionUtils.isEmpty(values)) {
                for (WxSendTemplateLogDTO value : values) {
                    SendTemplateLogVO templateLogVO = BeanConv.copy(value, SendTemplateLogVO.class);
                    templateLogVO.setJobTime(DateUtils.formatNewFormat(value.getJobTime()));
                    templateLogVO.setSendEndTime(DateUtils.formatNewFormat(value.getSendEndTime()));
                    templateLogVO.setSendStartTime(DateUtils.formatNewFormat(value.getSendStartTime()));
                    templateLogVOS.add(templateLogVO);
                }
            }
        }
        return PageViewBuilder.build(templateLogVOS, pageResult.getPage(), pageResult.getRows(), pageResult.getTotalRows());
    }

    @Override
    public Result<Boolean> cancelSend(TemplateCancelRequest request) {
        SendTemplateLogCancelRequest cancelRequest = new SendTemplateLogCancelRequest();
        cancelRequest.setId(request.getId());
        BaseResult baseResult = sendTemplateLogService.cancelSendLog(cancelRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<SendTemplateDetailVO> queryLogById(SendTemplateQueryRequest request) {
        PrimaryKeyRequest keyRequest = new PrimaryKeyRequest();
        keyRequest.setId(request.getId());
        SimpleResult<WxSendTemplateLogDTO> simpleResult = sendTemplateLogService.querySendLogById(keyRequest);
        SendTemplateDetailVO templateLogVO = null;
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            WxSendTemplateLogDTO data = simpleResult.getData();
            if (data != null) {
                templateLogVO = new SendTemplateDetailVO();
                templateLogVO.setId(data.getId());
                templateLogVO.setSendCondition(data.getSendCondition());
                templateLogVO.setTemplateLogType(data.getTemplateLogType());
            }
        }
        return JsonResultBuilder.succ(templateLogVO);
    }
}